home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / audacity / nyquist / nyquist.lsp < prev    next >
Encoding:
Lisp/Scheme  |  2010-09-21  |  53.8 KB  |  1,709 lines

  1. ;;;
  2. ;;;   ###########################################################
  3. ;;;   ### NYQUIST-- A Language for Composition and Synthesis. ###
  4. ;;;   ###                                                     ###
  5. ;;;   ### Copyright (c) 1994-2006 by Roger B. Dannenberg      ###
  6. ;;;   ###########################################################
  7. ;;;
  8. (load "fileio.lsp" :verbose NIL)
  9.  
  10. (prog ()
  11.    (setq lppp -12.0) (setq lpp -9.0)  (setq lp -6.0)    (setq lmp -3.0)
  12.    (setq lfff 12.0) (setq lff 9.0)  (setq lf 6.0)    (setq lmf 3.0)
  13.    (setq dB0 1.00)  (setq dB1 1.122) (setq dB10 3.1623)
  14.  
  15.    (setq s 0.25) (setq sd 0.375) (setq st (/ 0.5 3.0))
  16.    (setq i 0.5)  (setq id 0.75)  (setq it (* st 2.0))
  17.    (setq q 1.0)  (setq qd 1.5)   (setq qt (* st 4.0))
  18.    (setq h 2.0)  (setq hd 3.0)   (setq ht (* st 8.0))
  19.    (setq w 4.0)  (setq wd 6.0)   (setq wt (* st 16.0))
  20. )
  21.  
  22. (init-global *A4-Hertz* 440.0)
  23.  
  24. ; next pitch, for initializations below
  25. (defun np () (incf nyq:next-pitch))
  26.  
  27. (defun set-pitch-names ()
  28.    (setq no-pitch 116.0)
  29.    ; note: 58.0 is A4 - (C0 - 1) = 69 - (12 - 1)
  30.    (setf nyq:next-pitch (- (hz-to-step *A4-Hertz*) 58.0))
  31.  
  32.    (setf nyq:pitch-names
  33.     '(c0 (cs0 df0) d0 (ds0 ef0) e0 f0 (fs0 gf0) g0 (gs0 af0) a0
  34.       (as0 bf0) b0
  35.       c1 (cs1 df1) d1 (ds1 ef1) e1 f1 (fs1 gf1) g1 (gs1 af1) a1
  36.       (as1 bf1) b1
  37.       c2 (cs2 df2) d2 (ds2 ef2) e2 f2 (fs2 gf2) g2 (gs2 af2) a2
  38.       (as2 bf2) b2
  39.       c3 (cs3 df3) d3 (ds3 ef3) e3 f3 (fs3 gf3) g3 (gs3 af3) a3
  40.       (as3 bf3) b3
  41.       c4 (cs4 df4) d4 (ds4 ef4) e4 f4 (fs4 gf4) g4 (gs4 af4) a4
  42.       (as4 bf4) b4
  43.       c5 (cs5 df5) d5 (ds5 ef5) e5 f5 (fs5 gf5) g5 (gs5 af5) a5
  44.       (as5 bf5) b5
  45.       c6 (cs6 df6) d6 (ds6 ef6) e6 f6 (fs6 gf6) g6 (gs6 af6) a6
  46.       (as6 bf6) b6
  47.       c7 (cs7 df7) d7 (ds7 ef7) e7 f7 (fs7 gf7) g7 (gs7 af7) a7
  48.       (as7 bf7) b7))
  49.  
  50.    (dolist (p nyq:pitch-names)
  51.      (cond ((atom p) (set p (np)))
  52.        (t (let ((pitch (np)))
  53.         (dolist (s p) (set s pitch)))))))
  54.  
  55.  
  56. (set-pitch-names)
  57.  
  58. (init-global *default-sound-srate* 44100.0)
  59. (init-global *default-control-srate* 2205.0)
  60.  
  61. (setf *environment-variables*
  62.       '(*WARP* *SUSTAIN* *START* *LOUD* *TRANSPOSE* 
  63.     *STOP* *CONTROL-SRATE* *SOUND-SRATE*))
  64.  
  65. (setfn environment-time car)
  66. (setfn environment-stretch cadr)
  67.  
  68. ; ENVIRONMENT-MAP - map virtual time using an environment
  69. ;
  70. ;(defun environment-map (env tim)
  71. ;  (+ (environment-time env)
  72. ;     (* (environment-stretch env) tim)))
  73.  
  74.  
  75. (defun nyq:the-environment () (mapcar 'eval *environment-variables*))
  76.  
  77.  
  78. ;; GLOBAL ENVIRONMENT VARIABLES and their startup values:
  79. (defun nyq:environment-init ()
  80.   (setq *WARP*        '(0.0 1.0 nil))
  81.   (setq *LOUD*    0.0)   ; now in dB
  82.   (setq *TRANSPOSE*    0.0)
  83.   (setq *SUSTAIN*            1.0)
  84.   (setq *START*       MIN-START-TIME)
  85.   (setq *STOP*        MAX-STOP-TIME)
  86.   (setq *CONTROL-SRATE*  *DEFAULT-CONTROL-SRATE*)
  87.   (setq *SOUND-SRATE* *DEFAULT-SOUND-SRATE*)
  88.   t)                ; return nothing in particular
  89.  
  90. (nyq:environment-init)
  91.  
  92. (defun get-duration (dur)
  93.   (let ((duration 
  94.          (- (local-to-global (* (get-sustain) dur))
  95.             (setf *rslt* (local-to-global 0)))))
  96.      (cond ((minusp duration)
  97.             (error
  98. "duration is less than zero: perhaps a warp or stretch
  99. is ill-formed. Nyquist cannot continue because synthesis
  100. functions assume durations are always positive.")))
  101.      duration))
  102.  
  103.  
  104. (defun get-loud ()
  105.   (cond ((numberp *loud*) *loud*)
  106.     ((soundp *loud*)
  107.      (sref *loud* 0))
  108.     (t
  109.      (error (format t "*LOUD* should be a number or sound: ~A" *LOUD*)))))
  110.  
  111.  
  112. (defun get-sustain ()
  113.   (cond ((numberp *SUSTAIN*) *SUSTAIN*)
  114.     ((soundp *SUSTAIN*)
  115.      ;(display "get-sustain: lookup " (local-to-global 0) 0))
  116.      (sref *SUSTAIN* 0))
  117.     (t
  118.      (error (format t "*SUSTAIN* should be a number or sound: ~A" *SUSTAIN*)))))
  119.  
  120.  
  121. (defun get-tempo ()
  122.   (slope (snd-inverse (get-warp) (local-to-global 0)
  123.               *control-srate*)))
  124.  
  125. (defun get-transpose ()
  126.   (cond ((numberp *TRANSPOSE*) *TRANSPOSE*)
  127.     ((soundp *TRANSPOSE*)
  128.      ; (display "get-transpose: lookup " 0)
  129.      ; (format t "samples: ~A~%" (snd-samples *TRANSPOSE* 100))
  130.      (sref *TRANSPOSE* 0))
  131.     (t
  132.      (error (format t "*TRANSPOSE* should be a number or sound: ~A" *TRANSPOSE*)))))
  133.  
  134.  
  135. (defun get-warp ()
  136.   (let ((f (warp-function *WARP*)))
  137.     (cond ((null f) (error "Null warp function"))
  138.     (t
  139.      (shift-time (scale-srate f (/ (warp-stretch *WARP*)))
  140.              (- (warp-time *WARP*)))))))
  141.  
  142.  
  143. ;;;;;;;;;;;;;;;;;;;;;;
  144. ;; OSCILATORS
  145. ;;;;;;;;;;;;;;;;;;;;;;
  146.  
  147. (defun build-harmonic (n table-size) (snd-sine 0 n table-size 1))
  148.  
  149. (setf *SINE-TABLE* (list (build-harmonic 1 2048)
  150.              (hz-to-step 1.0)
  151.              T))
  152. (setf *TABLE* *SINE-TABLE*)
  153.  
  154.  
  155. ;; AMOSC
  156. ;;
  157. (defun amosc (pitch modulation &optional (sound *table*) (phase 0.0))
  158.   (let ((modulation-srate (snd-srate modulation))
  159.     (hz (step-to-hz (+ pitch (get-transpose)))))
  160.     (cond ((> *SOUND-SRATE* modulation-srate)
  161.        (setf modulation (snd-up *SOUND-SRATE* modulation)))
  162.       ((< *SOUND-SRATE* modulation-srate)
  163.        (format t "Warning: down-sampling AM modulation in amosc~%")
  164.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  165.     (cond ((> hz (/ *SOUND-SRATE* 2))
  166.        (format t "Warning: amosc frequency (~A hz) will alias at current sample rate (~A hz).\n"
  167.            hz *SOUND-SRATE*)))
  168.     (scale-db (get-loud)
  169.       (snd-amosc
  170.     (car sound)    ; samples for table
  171.     (cadr sound)    ; step represented by table
  172.     *SOUND-SRATE*    ; output sample rate
  173.     hz        ;  output hz
  174.     (local-to-global 0)    ; starting time
  175.     modulation    ; modulation
  176.     phase))))    ; phase
  177.  
  178.  
  179. ;; FMOSC
  180. ;;
  181. ;; modulation rate must be less than or equal to sound-srate, so
  182. ;; force resampling and issue a warning if necessary. snd-fmosc can
  183. ;; handle upsampling cases internally.
  184. ;;
  185. (defun fmosc (pitch modulation &optional (sound *table*) (phase 0.0))
  186.   (let ((modulation-srate (snd-srate modulation))
  187.         (hz (step-to-hz (+ pitch (get-transpose)))))
  188.     (cond ((< *SOUND-SRATE* modulation-srate)
  189.        (format t "Warning: down-sampling FM modulation in fmosc~%")
  190.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  191.     (cond ((> hz (/ *SOUND-SRATE* 2))
  192.        (format t "Warning: fmosc nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  193.            hz *SOUND-SRATE*)))
  194.     (scale-db (get-loud)
  195.       (snd-fmosc 
  196.         (car sound)        ; samples for table
  197.         (cadr sound)        ; step represented by table
  198.         *SOUND-SRATE*        ; output sample rate
  199.         hz            ;  output hz
  200.         (local-to-global 0)    ; starting time
  201.         modulation        ; modulation
  202.         phase))))        ; phase
  203.  
  204.  
  205. ;; FMFB
  206. ;;
  207. ;; this code is based on FMOSC above
  208. ;;
  209. (defun fmfb (pitch index &optional dur)
  210.  (let ((hz (step-to-hz (+ pitch (get-transpose)))))
  211.    (cond ((> hz (/ *SOUND-SRATE* 2))
  212.           (format "Warning: fmfb nominal frequency (~A hz) will alias at current sample rate (~A hz).~%"
  213.                   hz *SOUND-SRATE*)))
  214.    (setf dur (get-duration dur))
  215.    (cond ((soundp index) (ny:fmfbv hz index))
  216.           (t
  217.            (scale-db (get-loud)
  218.                      (snd-fmfb (local-to-global 0) 
  219.                                hz *SOUND-SRATE* index dur))))))
  220.  
  221. ;; private variable index version of fmfb
  222. (defun ny:fmfbv (hz index)
  223.   (let ((modulation-srate (snd-srate index)))
  224.     (cond ((< *SOUND-SRATE* modulation-srate)
  225.            (format t "Warning: down-sampling FM modulation in fmfb~%")
  226.            (setf index (snd-down *SOUND-SRATE* index))))
  227.     (scale-db (get-loud)
  228.               (snd-fmfbv (local-to-global 0) hz *SOUND-SRATE* index))))
  229.  
  230.  
  231. ;; BUZZ
  232. ;;
  233. ;; (ARGUMENTS ("long" "n") ("rate_type" "sr") ("double" "hz")
  234. ;;            ("time_type" "t0") ("sound_type" "s_fm"))
  235. ;; 
  236. (defun buzz (n pitch modulation)
  237.   (let ((modulation-srate (snd-srate modulation))
  238.         (hz (step-to-hz (+ pitch (get-transpose)))))
  239.     (cond ((< *SOUND-SRATE* modulation-srate)
  240.            (format t "Warning: down-sampling modulation in buzz~%")
  241.            (setf modulation (snd-down *SOUND-SRATE* modulation))))
  242.     (cond ((> hz (/ *SOUND-SRATE* 2))
  243.            (format t "Warning: buzz nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  244.                    hz *SOUND-SRATE*)))
  245.     (setf n (max n 1)) ; avoid divide by zero problem
  246.     (scale-db (get-loud)
  247.               (snd-buzz n                   ; number of harmonics
  248.                         *SOUND-SRATE*       ; output sample rate
  249.                         hz                  ; output hz
  250.                         (local-to-global 0) ; starting time
  251.                         modulation))))      ; freq. modulation
  252.                         
  253.  
  254. ;; (HZOSC hz [table [phase]])
  255. ;;
  256. ;; similar to FMOSC, but without "carrier" frequency parameter
  257. ;; also, hz may be a scalar or a sound
  258. ;;
  259. (defun hzosc (hz &optional (sound *table*) (phase 0.0))
  260.   (let (hz-srate)
  261.     (cond ((numberp hz)
  262.            (osc (hz-to-step hz) 1.0 sound phase))
  263.           (t
  264.            (setf hz-srate (snd-srate hz))
  265.            (cond ((< *SOUND-SRATE* hz-srate)
  266.                   (format t "Warning: down-sampling hz in hzosc~%")
  267.                   (setf hz (snd-down *SOUND-SRATE* hz))))
  268.            (scale-db (get-loud)
  269.                      (snd-fmosc (car sound) ; samples for table
  270.                                 (cadr sound) ; step repr. by table
  271.                                 *SOUND-SRATE* ; output sample rate
  272.                                 0.0 ; dummy carrier
  273.                                 (local-to-global 0) ; starting time
  274.                                 hz phase))))))
  275.  
  276.  
  277. ;; (SIOSC-BREAKPOINTS tab0 t1 tab1 ... tn tabn)
  278. ;;   converts times to sample numbers
  279. ;; NOTE: time-warping the spectral envelope seems
  280. ;; like the wrong thing to do (wouldn't it be better
  281. ;; to warp the parameters that control the spectra,
  282. ;; or don't warp at all?). Nominally, a note should
  283. ;; have a "score" or local time duration equal to the
  284. ;; SUSTAIN environment variable. (When sustain is 1.0
  285. ;; and no time-warping is in effect, the duration is 1).
  286. ;; So, scale all times by
  287. ;;        (local-to-global (get-sustain))
  288. ;; so that if the final time tn = 1.0, we get a nominal
  289. ;; length note.
  290.  
  291. (defun siosc-breakpoints (breakpoints)
  292.   (display "siosc-breakpoints" breakpoints)
  293.   (prog (sample-count result (last-count 0) time-factor)
  294.     (setf time-factor
  295.       (- (local-to-global (get-sustain))
  296.          (local-to-global 0.0)))
  297.     (setf time-factor (* time-factor *SOUND-SRATE*))
  298.     (cond ((and (listp breakpoints)
  299.         (cdr breakpoints)
  300.         (cddr breakpoints)))
  301.       (t (error "SIOSC table list must have at least 3 elements")))
  302. loop
  303.     (cond ((and (listp breakpoints)
  304.            (soundp (car breakpoints)))
  305.        (push (car breakpoints) result)
  306.        (setf breakpoints (cdr breakpoints)))
  307.       (t
  308.        (error "SIOSC expecting SOUND in table list")))
  309.     (cond ((and breakpoints
  310.         (listp breakpoints)
  311.         (numberp (car breakpoints)))
  312.        (setf sample-count (truncate
  313.         (+ 0.5 (* time-factor (car breakpoints)))))
  314.        (cond ((< sample-count last-count)
  315.           (setf sample-count (1+ last-count))))
  316.        (push sample-count result)
  317.        (setf last-count sample-count)
  318.        (setf breakpoints (cdr breakpoints))
  319.        (cond (breakpoints
  320.           (go loop))))
  321.       (breakpoints
  322.        (error "SIOSC expecting number (time) in table list")))
  323.     (setf result (reverse result))
  324.     (display "siosc-breakpoints" result)
  325.     (return result)))
  326.  
  327. ;; SIOSC -- spectral interpolation oscillator
  328. ;;
  329. ;; modulation rate must be less than or equal to sound-srate, so
  330. ;; force resampling and issue a warning if necessary. snd-fmosc can
  331. ;; handle upsampling cases internally.
  332. ;;
  333. (defun siosc (pitch modulation breakpoints)
  334.   (let ((modulation-srate (snd-srate modulation))
  335.     (hz (step-to-hz (+ pitch (get-transpose)))))
  336.     (cond ((< *SOUND-SRATE* modulation-srate)
  337.        (format t "Warning: down-sampling FM modulation in siosc~%")
  338.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  339.     (cond ((> hz (/ *SOUND-SRATE* 2))
  340.        (format t "Warning: siosc nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  341.            hz *SOUND-SRATE*)))
  342.      (scale-db (get-loud)
  343.       (snd-siosc 
  344.     (siosc-breakpoints breakpoints)    ; tables
  345.     *SOUND-SRATE*        ; output sample rate
  346.     hz            ;  output hz
  347.     (local-to-global 0)    ; starting time
  348.     modulation))))        ; modulation
  349.  
  350.  
  351. ;; LFO -- freq &optional duration sound phase)
  352. ;;
  353. ;; Default duration is 1.0 sec, default sound is *TABLE*, 
  354. ;; default phase is 0.0.
  355. ;;
  356. (defun lfo (freq &optional (duration 1.0)
  357.          (sound *SINE-TABLE*) (phase 0.0))
  358.   (let ((d (get-duration duration)))
  359.     (if (minusp d) (setf d 0))
  360.     (cond ((> freq (/ *CONTROL-SRATE* 2))
  361.            (format t "Warning: lfo frequency (~A hz) will alias at current control rate (~A hz).\n"
  362.                      freq *CONTROL-SRATE*)))
  363.     (set-logical-stop
  364.       (snd-osc
  365.         (car sound)        ; samples for table
  366.         (cadr sound)        ; step represented by table
  367.         *CONTROL-SRATE*        ; output sample rate
  368.         freq            ; output hz
  369.         *rslt*            ; starting time
  370.         d            ; duration
  371.         phase)                ; phase
  372.       duration)))
  373.  
  374.  
  375. ;; FMLFO -- like LFO but uses frequency modulation
  376. ;;
  377. (defun fmlfo (freq &optional (sound *SINE-TABLE*) (phase 0.0))
  378.   (let ()
  379.     (cond ((numberp freq)
  380.            (lfo freq 1.0 sound phase))
  381.           ((soundp freq)
  382.            (cond ((> (snd-srate freq) *CONTROL-SRATE*)
  383.                   (setf freq (force-srate *CONTROL-SRATE* freq))))
  384.            (snd-fmosc (car sound) (cadr sound) *CONTROL-SRATE* 0.0 
  385.                       (local-to-global 0) freq phase))
  386.           (t
  387.            (error "frequency must be a number or sound")))))
  388.  
  389.  
  390. ;; OSC - table lookup oscillator
  391. ;;
  392. (defun osc (pitch &optional (duration 1.0) 
  393.             (sound *TABLE*) (phase 0.0))
  394.   (let ((d  (get-duration duration))
  395.         (hz (step-to-hz (+ pitch (get-transpose)))))
  396.     ;(display "osc" *warp* global-start global-stop actual-dur  
  397.     ;         (get-transpose))
  398.     (cond ((> hz (/ *SOUND-SRATE* 2))
  399.            (format t "Warning: osc frequency (~A hz) will alias at current sample rate (~A hz).\n"
  400.                      hz *SOUND-SRATE*)))
  401.     (set-logical-stop
  402.       (scale-db (get-loud)
  403.         (snd-osc 
  404.           (car sound)        ; samples for table
  405.           (cadr sound)        ; step represented by table
  406.           *SOUND-SRATE*        ; output sample rate
  407.           hz            ;  output hz
  408.           *rslt*        ; starting time
  409.           d            ; duration
  410.           phase))               ; phase
  411.       duration)))
  412.  
  413.  
  414. ;; PARTIAL -- sine osc with built-in envelope scaling
  415. ;;
  416. (defun partial (steps env)
  417.   (let ((hz (step-to-hz (+ steps (get-transpose)))))
  418.     (cond ((> hz (/ *sound-srate* 2))
  419.            (format t "Warning: partial frequency (~A hz) will alias at current sample rate (~A hz).\n"
  420.                      hz *sound-srate*)))
  421.     (scale-db (get-loud)
  422.       (snd-partial *sound-srate* hz
  423.                    (force-srate *sound-srate* env)))))
  424.  
  425.  
  426. ;; SAMPLER -- simple attack + sustain sampler
  427. ;;
  428. (defun sampler (pitch modulation 
  429.                 &optional (sample *table*) (npoints 2))
  430.   (let ((samp (car sample))
  431.     (samp-pitch (cadr sample))
  432.     (samp-loop-start (caddr sample))
  433.     (hz (step-to-hz (+ pitch (get-transpose)))))
  434.     ; make a waveform table look like a sample with no attack:
  435.     (cond ((not (numberp samp-loop-start))
  436.            (setf samp-loop-start 0.0)))
  437.     (cond ((> hz (/ *SOUND-SRATE* 2))
  438.            (format t "Warning: sampler nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  439.                      hz *SOUND-SRATE*)))
  440.     (scale-db (get-loud)
  441.        (snd-sampler 
  442.         samp        ; samples for table
  443.         samp-pitch    ; step represented by table
  444.         samp-loop-start ; time to start loop
  445.         *SOUND-SRATE*    ; output sample rate
  446.         hz        ;  output hz
  447.         (local-to-global 0)    ; starting time
  448.         modulation    ; modulation
  449.         npoints))))        ; number of interpolation points
  450.  
  451.  
  452. ;; SINE -- simple sine oscillator
  453. ;;
  454. (defun sine (steps &optional (duration 1.0))
  455.   (let ((hz (step-to-hz (+ steps (get-transpose))))
  456.         (d (get-duration duration)))
  457.     (cond ((> hz (/ *SOUND-SRATE* 2))
  458.            (format t "Warning: sine frequency (~A hz) will alias at current sample rate (~A hz).\n"
  459.                      hz *SOUND-SRATE*)))
  460.     (set-logical-stop
  461.       (scale-db (get-loud)
  462.         (snd-sine *rslt* hz *sound-srate* d))
  463.       duration)))
  464.  
  465.  
  466. ;; PLUCK
  467. ;;
  468. ;; (ARGUMENTS ("double" "sr") ("double" "hz") ("time_type" "t0") 
  469. ;;            ("time_type" "d") ("double" "final_amp"))
  470. ;;
  471. (defun pluck (steps &optional (duration 1.0) (final-amp 0.001))
  472.   (let ((hz (step-to-hz (+ steps (get-transpose))))
  473.         (d (get-duration duration)))
  474.     (cond ((> hz (/ *SOUND-SRATE* 2))
  475.            (format t "Warning: pluck frequency (~A hz) will alias at current sample rate (~A hz).\n"
  476.                      hz *SOUND-SRATE*)))
  477.     (set-logical-stop
  478.       (scale-db (get-loud)
  479.         (snd-pluck *SOUND-SRATE* hz *rslt* d final-amp))
  480.       duration)))
  481.  
  482.  
  483. ;; abs-env -- restore the standard environment
  484. ;;
  485. (defmacro abs-env (s)
  486.   `(progv '(*WARP* *LOUD* *TRANSPOSE* *SUSTAIN* 
  487.             *START* *STOP*
  488.             *CONTROL-SRATE* *SOUND-SRATE*)
  489.           (list '(0.0 1.0 NIL) 0.0 0.0 1.0
  490.            MIN-START-TIME MAX-STOP-TIME
  491.            *DEFAULT-CONTROL-SRATE* *DEFAULT-SOUND-SRATE*)
  492.      ,s))
  493.  
  494.  
  495. ; nyq:add2 - add two arguments
  496. (defun nyq:add2 (s1 s2)
  497.   (cond ((and (arrayp s1) (not (arrayp s2)))
  498.          (setf s2 (vector s2)))
  499.         ((and (arrayp s2) (not (arrayp s1)))
  500.          (setf s1 (vector s1))))
  501.   (cond ((arrayp s1)
  502.          (sum-of-arrays s1 s2))
  503.         (t
  504.          (nyq:add-2-sounds s1 s2))))
  505.  
  506.  
  507. ; (NYQ:ADD-2-SOUNDS S1 S2) - add two sound (or number) arguments
  508. (defun nyq:add-2-sounds (s1 s2)
  509.   (cond ((numberp s1)
  510.          (cond ((numberp s2)
  511.         (+ s1 s2))
  512.           (t
  513.            (snd-offset s2 s1))))
  514.     ((numberp s2)
  515.      (snd-offset s1 s2))
  516.     (t
  517.      (let ((s1sr (snd-srate s1))
  518.            (s2sr (snd-srate s2)))
  519. ;    (display "nyq:add-2-sounds" s1sr s2sr)
  520.        (cond ((> s1sr s2sr)
  521.               (snd-add s1 (snd-up s1sr s2)))
  522.              ((< s1sr s2sr)
  523.               (snd-add (snd-up s2sr s1) s2))
  524.              (t
  525.               (snd-add s1 s2)))))))
  526.  
  527.  
  528. (defmacro at (x s)
  529.  `(progv '(*WARP*) (list (list (+ (warp-time *WARP*) 
  530.                   (* (warp-stretch *WARP*) ,x))
  531.                    (warp-stretch *WARP*)
  532.                    (warp-function *WARP*)))
  533.       ,s))
  534.  
  535.  
  536. ;; (AT-ABS t behavior) evaluate behavior at global time t
  537. ;;
  538. ;; *WARP* is the triple (d s f) denoting the function f(st+d),
  539. ;; a mapping from local to global time.
  540. ;; We want (d' s f) such that f(s*0 + d') = t
  541. ;; (Note that we keep the same s and f, and only change the offset.
  542. ;; To eliminate the warp and stretch use "(abs-env (at t behavior))")
  543. ;; Applying the inverse of f, d' = f-1(t), or (sref (snd-inverse f ...) t)
  544. ;; Rather than invert the entire function just to evaluate at one point,
  545. ;; we use SREF-INVERSE to find d'.
  546. ;;
  547. (defmacro at-abs (x s)
  548.  `(progv '(*WARP*)
  549.      (if (warp-function *WARP*)
  550.                (list (list (sref-inverse (warp-function *WARP*) ,x)
  551.                    (warp-stretch *WARP*)
  552.                    (warp-function *WARP*)))
  553.                (list (list ,x (warp-stretch *WARP*) NIL)))
  554.      ,s))
  555.  
  556. ;; (CLIP S1 VALUE) - clip maximum amplitude to value
  557. ;
  558. (defun clip (x v)
  559.   (cond ((numberp x)
  560.      (max (min x v) (- v)))
  561.     ((arrayp x)
  562.      (let* ((len (length x))
  563.         (result (make-array len)))
  564.         (dotimes (i len)
  565.         (setf (aref result i) 
  566.               (snd-clip (aref x i) v)))
  567.         result))
  568.     (t
  569.      (snd-clip x v))))
  570.  
  571.  
  572. ;; (NYQ:COERCE-TO S1 S2) - expand sound s1 to type of s2
  573. (defun nyq:coerce-to (s1 s2)
  574.   (cond ((or (soundp s1) (numberp s1))
  575.          (cond ((arrayp s2)
  576.                 (nyq:sound-to-array s1 (length s2)))
  577.                (t s1)))
  578.          (t s1)))
  579.  
  580.  
  581. (defmacro continuous-control-warp (beh)
  582.   `(snd-compose (warp-abs nil ,beh)
  583.         (snd-inverse (get-warp)
  584.          (local-to-global 0) *control-srate*)))
  585.  
  586. (defmacro continuous-sound-warp (beh)
  587.   `(snd-compose (warp-abs nil ,beh)
  588.         (snd-inverse (get-warp)
  589.          (local-to-global 0) *sound-srate*)))
  590.  
  591.  
  592. (defmacro control-srate-abs (r s)
  593.   `(progv '(*CONTROL-SRATE*) (list ,r)
  594.       ,s))
  595.  
  596. ; db = 20log(ratio)
  597. ; db = 20 ln(ratio)/ln(10)
  598. ; db/20 = ln(ratio)/ln(10)
  599. ; db ln(10)/20 = ln(ratio)
  600. ; e^(db ln(10)/20) = ratio
  601. ;
  602. (setf ln10over20 (/ (log 10.0) 20))
  603.  
  604. (defun db-to-linear (x) 
  605.   (cond ((numberp x)
  606.      (exp (* ln10over20 x)))
  607.     ((arrayp x)
  608.      (let* ((len (length x))
  609.         (result (make-array len)))
  610.         (dotimes (i len)
  611.         (setf (aref result i) 
  612.               (snd-exp (snd-scale ln10over20 (aref x i)))))
  613.         result))
  614.     (t
  615.      (snd-exp (snd-scale ln10over20 x)))))
  616.  
  617.  
  618. (defun linear-to-db (x) 
  619.   (cond ((numberp x)
  620.      (/ (log (float x)) ln10over20))
  621.     ((arrayp x)
  622.      (let* ((len (length x))
  623.         (result (make-array len)))
  624.         (dotimes (i len)
  625.         (setf (aref result i) 
  626.               (snd-scale (/ 1.0 ln10over20) (snd-log (aref x i)))))
  627.         result))
  628.     (t
  629.      (snd-scale (/ 1.0 ln10over20) (snd-log x)))))
  630.  
  631.  
  632. (cond ((not (fboundp 'scalar-step-to-hz))
  633.        (setfn scalar-step-to-hz step-to-hz)
  634.        (setfn scalar-hz-to-step hz-to-step)))
  635.  
  636.  
  637. (defun step-to-hz (x)
  638.   (cond ((numberp x)
  639.          (scalar-step-to-hz x))
  640.         ((arrayp x)
  641.          (let* ((len (length x))
  642.                 (result (make-array len)))
  643.            (dotimes (i len)
  644.              (setf (aref result i) (step-to-hz (aref x i))))
  645.            result))
  646.         (t
  647.          (s-exp (snd-offset (snd-scale 0.0577622650466621 x) 
  648.                             2.1011784386926213)))))
  649.  
  650. (defun hz-to-step (x)
  651.   (cond ((numberp x)
  652.          (scalar-hz-to-step x))
  653.         ((arrayp x)
  654.          (let* ((len (length x))
  655.                 (result (make-array len)))
  656.            (dotimes (i len)
  657.              (setf (aref result i) (hz-to-step (aref x i))))
  658.            result))
  659.         (t
  660.          (snd-scale 17.312340490667565
  661.                     (snd-offset (s-log x) -2.1011784386926213))))) 
  662.  
  663.  
  664. ; sref - access a sound at a given time point
  665. ;    note that the time is transformed to global
  666. (defun sref (sound point)
  667.   (snd-sref sound (local-to-global point)))
  668.  
  669.  
  670. ; extract - start is stretched and shifted as is stop
  671. ;  result is shifted to start at local time zero
  672. (defun extract (start stop sound)
  673.   (snd-xform sound (snd-srate sound) (local-to-global 0) 
  674.          (local-to-global start) (local-to-global stop) 1.0))
  675.  
  676. (defun extract-abs (start stop sound)
  677.   (snd-xform sound (snd-srate sound) 0 start stop 1.0))
  678.      
  679.  
  680. (defun local-to-global (local-time)
  681.   (let ((d (warp-time *WARP*))
  682.     (s (warp-stretch *WARP*))
  683.     (w (warp-function *WARP*))
  684.     global-time)
  685.     (setf global-time (+ (* s local-time) d))
  686.     (if w (snd-sref w global-time) global-time)))
  687.  
  688.  
  689. (defmacro loud (x s)
  690.  `(progv '(*LOUD*) (list (sum *LOUD* ,x))
  691.      ,s))
  692.  
  693.  
  694. (defmacro loud-abs (x s)
  695.  `(progv '(*LOUD*) (list ,x)
  696.      ,s))
  697.  
  698. (defun must-be-sound (x)
  699.  (cond ((soundp x) x)
  700.        (t
  701.     (error "SOUND type expected" x))))
  702.  
  703. ;; SCALE-DB -- same as scale, but argument is in db
  704. ;;
  705. (defun scale-db (factor sound)
  706.   (scale (db-to-linear factor) sound))
  707.  
  708. (defun set-control-srate (rate)
  709.   (setf *default-control-srate* (float rate))
  710.   (nyq:environment-init))
  711.  
  712. (defun set-sound-srate (rate) 
  713.   (setf *default-sound-srate* (float rate))
  714.   (nyq:environment-init))
  715.  
  716.  
  717. ; s-plot -- compute and write n data points for plotting
  718. ;
  719. ; dur is how many seconds of sound to plot. If necessary, cut the
  720. ;     sample rate to allow plotting dur seconds
  721. ; n is the number of points to plot. If there are more than n points,
  722. ;     cut the sample rate. If there are fewer than n samples, just
  723. ;     plot the points that exist.
  724. ;
  725. (defun s-plot (snd &optional (dur 2.0) (n 1000))
  726.   (prog* ((sr (snd-srate snd))
  727.           (t0 (snd-t0 snd))
  728.           (filename (soundfilename *default-plot-file*))
  729.           (s snd) ;; s is either snd or resampled copy of snd
  730.           (outf (open filename :direction :output)) ;; for plot data
  731.           (maximum -1000000.0) ;; maximum amplitude
  732.           (minimum  1000000.0) ;; minimum amplitude
  733.           actual-dur ;; is the actual-duration of snd
  734.           sample-count ;; is how many samples to get from s
  735.           period  ;; is the period of samples to be plotted
  736.           truncation-flag     ;; true if we didn't get whole sound
  737.           points) ;; is array of samples
  738.      ;; If we need more than n samples to get dur seconds, resample
  739.      (cond ((< n (* dur sr))
  740.             (setf s (force-srate (/ (float n) dur) snd))))
  741.      ;; Get samples from the signal
  742.      (setf points (snd-samples s (1+ n)))
  743.      ;; If we got fewer than n points, we can at least estimate the
  744.      ;; actual duration (we might not know exactly if we use a lowered
  745.      ;; sample rate). If the actual sample rate was lowered to avoid
  746.      ;; getting more than n samples, we can now raise the sample rate
  747.      ;; based on our estimate of the actual sample duration.
  748.      (display "test" (length points) n)
  749.      (cond ((< (length points) n)
  750.             ;; sound is shorter than dur, estimate actual length
  751.             (setf actual-dur (/ (length points) (snd-srate s)))
  752.             (setf sample-count (round (min n (* actual-dur sr))))
  753.             (cond ((< n (* actual-dur sr))
  754.                    (setf s (force-srate (/ (float n) actual-dur) snd)))
  755.                   (t ;; we can use original signal
  756.                    (setf s snd)))
  757.             (setf points (snd-samples s sample-count))
  758.             ;; due to rounding, need to recalculate exact count
  759.             (setf sample-count (length points)))
  760.            ((= (length points) n)
  761.             (setf actual-dur dur)
  762.             (setf sample-count n))
  763.            (t ;; greater than n points, so we must have truncated sound
  764.             (setf actual-dur dur)
  765.             (setf sample-count n)
  766.             (setf truncation-flag t)))
  767.      ;; actual-dur is the duration of the plot
  768.      ;; sample-count is how many samples we have
  769.      (setf period (/ 1.0 (snd-srate s)))
  770.      (cond ((null outf)
  771.             (format t "s-plot: could not open ~A!~%" filename)
  772.             (return nil)))
  773.     (format t "s-plot: writing ~A ... ~%" filename)
  774.     (cond (truncation-flag
  775.            (format t "        !!TRUNCATING SOUND TO ~As\n" actual-dur)))
  776.     (cond ((/= (snd-srate s) (snd-srate snd))
  777.            (format t "        !!RESAMPLING SOUND FROM ~A to ~Ahz\n"
  778.                    (snd-srate snd) (snd-srate s))))
  779.     (cond (truncation-flag
  780.            (format t "        Plotting ~As, actual sound duration is greater\n"
  781.                      actual-dur))
  782.           (t
  783.            (format t "        Sound duration is ~As~%" actual-dur)))
  784.     (dotimes (i sample-count)
  785.       (setf maximum (max maximum (aref points i)))
  786.       (setf minimum (min minimum (aref points i)))
  787.       (format outf "~A ~A~%" (+ t0 (* i period)) (aref points i)))
  788.     (close outf)
  789.     (format t "        Wrote ~A points from ~As to ~As~%" 
  790.               sample-count t0 (+ t0 actual-dur))
  791.     (format t "        Range of values ~A to ~A\n" minimum maximum)
  792.     (cond ((or (< minimum -1) (> maximum 1))
  793.            (format t "        !!SIGNAL EXCEEDS +/-1~%")))))
  794.  
  795.  
  796. ; run something like this to plot the points:
  797. ; graph < points.dat | plot -Ttek
  798.  
  799.  
  800. (defmacro sound-srate-abs (r s)
  801.   `(progv '(*SOUND-SRATE*) (list ,r)
  802.       ,s))
  803.  
  804.  
  805. (defmacro stretch (x s)
  806.  `(progv '(*WARP*) (list (list (warp-time *WARP*) 
  807.                    (* (warp-stretch *WARP*) ,x)
  808.                    (warp-function *WARP*)))
  809.      (if (minusp (warp-stretch *WARP*))
  810.          (break "Negative stretch factor is not allowed"))
  811.              ,s))
  812.  
  813.          
  814. (defmacro stretch-abs (x s)
  815.  `(progv '(*WARP*) (list (list (local-to-global 0)
  816.                    ,x
  817.                    nil))
  818.      (if (minusp (warp-stretch *WARP*))
  819.          (break "Negative stretch factor is not allowed"))
  820.              ,s))
  821.  
  822.  
  823. (defmacro sustain (x s)
  824.  `(progv '(*SUSTAIN*) (list (prod *SUSTAIN* ,x))
  825.       ,s))
  826.  
  827.  
  828. (defmacro sustain-abs (x s)
  829.  `(progv '(*SUSTAIN*) (list ,x)
  830.       ,s))
  831.  
  832.  
  833. ;; (WARP-FUNCTION *WARP*) - extracts function field of warp triple
  834. ;;
  835. (setfn warp-function caddr)
  836.  
  837.  
  838. ;; (WARP-STRETCH *WARP*) - extracts stretch field of warp triple
  839. ;;
  840. (setfn warp-stretch cadr)
  841.  
  842.  
  843. ;; (WARP-TIME *WARP*) - extracts time field of warp triple
  844. ;;
  845. (setfn warp-time car)
  846.  
  847.  
  848. (defmacro transpose (x s)
  849.  `(progv '(*TRANSPOSE*) (list (sum *TRANSPOSE* ,x))
  850.       ,s))
  851.  
  852.  
  853. (defmacro transpose-abs (x s)
  854.  `(progv '(*TRANSPOSE*) (list ,x)
  855.       ,s))
  856.  
  857.  
  858. ;; COMPUTE-DEFAULT-SOUND-FILE -- construct and set *default-sound-file*
  859. ;;
  860. ;; (this is harder than it might seem because the default place for
  861. ;;  sound files is in /tmp, which is shared by users, so we'd like to
  862. ;;  use a user-specific name to avoid collisions)
  863. ;;
  864. (defun compute-default-sound-file () 
  865.   (let (inf user extension)
  866.       ; the reason for the user name is that if UserA creates a temp file,
  867.       ; then UserB will not be able to overwrite it. The user name is a
  868.       ; way to give each user a unique temp file name. Note that we don't
  869.       ; want each session to generate a unique name because Nyquist doesn't
  870.       ; delete the sound file at the end of the session.
  871.    (setf user (get-user))
  872. #|
  873.    (cond ((null user)           
  874.        (format t 
  875. "Please type your user-id so that I can construct a default 
  876. sound-file name.  To avoid this message in the future, add
  877. this to your .login file:
  878.     setenv USER <your id here>
  879. or add this to your init.lsp file:
  880.     (setf *default-sound-file* \"<your filename here>\")
  881.     (setf *default-sf-dir* \"<full pathname of desired directory here>\")
  882.  
  883. Your id please: ")
  884.        (setf user (read))))
  885. |#
  886.     ; now compute the extension based on *default-sf-format*
  887.     (cond ((= *default-sf-format* snd-head-AIFF)
  888.            (setf extension ".aif"))
  889.           ((= *default-sf-format* snd-head-Wave)
  890.            (setf extension ".wav"))
  891.           (t
  892.            (setf extension ".snd")))
  893.     (setf *default-sound-file* 
  894.       (strcat (string-downcase user) "-temp" extension))
  895.     (format t "Default sound file is ~A.~%" *default-sound-file*)))
  896.  
  897.  
  898. ;; CONTROL-WARP -- apply a warp function to a control function
  899. ;; 
  900. (defun control-warp (warp-fn control &optional wrate)
  901.   (cond (wrate
  902.      (snd-resamplev control *control-srate*
  903.             (snd-inverse warp-fn (local-to-global 0) wrate)))
  904.     (t
  905.      (snd-compose control
  906.               (snd-inverse warp-fn (local-to-global 0) *control-srate*)))))
  907.  
  908.  
  909. ;; (cue sound)
  910. ;;    Cues the given sound; that is, it applies the current *WARP*, *LOUD*,
  911. ;; *START*, and *STOP* values to the argument.  The logical start time is at
  912. ;; local time 0.
  913. (defun cue (sound)
  914.   (cond ((arrayp sound)
  915.      (let* ((len (length sound))
  916.         (result (make-array len)))
  917.         (dotimes (i len)
  918.         (setf (aref result i)
  919.               (cue-sound (aref sound i))))
  920.         result))
  921.     (t
  922.      (cue-sound sound))))
  923.  
  924. (defun cue-sound (sound)
  925.   (snd-xform sound
  926.          (snd-srate sound)
  927.          (local-to-global 0) *START* *STOP* (db-to-linear (get-loud))))
  928.  
  929. ;; (sound sound)
  930. ;;    Same as (cue sound), except also warps the sound.
  931. ;; Note that the *WARP* can change the pitch of the
  932. ;; sound as a result of resampling.
  933. ;; Here's the derivation for the warping code:
  934. ;; *WARP* is a triple: (d s f) which denotes that the warp from local to
  935. ;; global time is: f(st+d)
  936. ;; We need to compose sound with the inverse of this to get a function
  937. ;; of global time
  938. ;; Let f-1 be the inverse of f.  Then the inverse of f(st+d) is 
  939. ;; (f-1(t) - d)/s
  940. ;; The composition gives us: (snd-compose sound (f-1(t) - d)/s)
  941. ;; Eliminate the 1/s term by changing the sample rate of sound:
  942. ;;  = (snd-compose (snd-scale-srate sound s) (f-1(t) - d))
  943. ;; Eliminate the -d term by shifting f before taking the inverse:
  944. ;;  = (snd-compose (scale-srate sound s) ((inverse f) - d))
  945. ;;  = (snd-compose (scale-srate sound s) (inverse f(t + d)))
  946. ;;  = (snd-compose (scale-srate sound s) (inverse (shift f -d)))
  947. ;; snd-inverse takes a time and sample rate.  For time, use zero.
  948. ;; The sample rate of inverse determines the final sample rate of
  949. ;; this function, so use *SOUND-SRATE*:
  950. ;;  = (snd-compose (scale-srate sound s) (snd-inverse (shift-time f (- d))
  951. ;;                                              0 *SOUND-SRATE*))
  952. ;;
  953. (defun nyq:sound (sound)
  954.    (cond ((null (warp-function *WARP*))
  955.       (snd-xform sound (/ (snd-srate sound) (warp-stretch *WARP*))
  956.              (local-to-global 0)
  957.              *START* *STOP* (db-to-linear (get-loud))))
  958.      (t
  959.       (snd-compose (scale-srate sound (warp-stretch *WARP*))
  960.                (snd-inverse (shift-time (warp-function *WARP*)
  961.                         (- (warp-time *WARP*)))
  962.                     0 *SOUND-SRATE*)))))
  963.  
  964. (defun nyq:sound-of-array (sound)
  965.   (let* ((n (length sound))
  966.          (s (make-array n)))
  967.     (dotimes (i n)
  968.       (setf (aref s i) (nyq:sound (aref sound i))))
  969.     s))
  970.  
  971.  
  972. (defun sound (sound)
  973.   (cond ((arrayp sound)
  974.      (nyq:sound-of-array sound))
  975.     (t
  976.      (nyq:sound sound))))
  977.  
  978.  
  979. ;; (SCALE-SRATE SOUND SCALE)
  980. ;; multiplies the sample rate by scale
  981. (defun scale-srate (sound scale)
  982.   (let ((new-srate (* scale (snd-srate sound))))
  983.     (snd-xform sound new-srate (snd-time sound) 
  984.            MIN-START-TIME MAX-STOP-TIME 1.0)))
  985.  
  986.  
  987. ;; (SHIFT-TIME SOUND SHIFT)
  988. ;; shift the time of a function by SHIFT, i.e. if SOUND is f(t),
  989. ;; then (shift-time SOUND SHIFT) is f(t - SHIFT).  Note that if
  990. ;; you look at plots, the shifted sound will move *right* when SHIFT
  991. ;; is positive.  
  992. (defun shift-time (sound shift)
  993.   (snd-xform sound (snd-srate sound) (+ (snd-t0 sound) shift)
  994.          MIN-START-TIME MAX-STOP-TIME 1.0))
  995.  
  996.  
  997. ;; (NYQ:SOUND-TO-ARRAY SOUND N) - duplicate SOUND to N channels
  998. ;;
  999. (defun nyq:sound-to-array (sound n)
  1000.   (let ((result (make-array n)))
  1001.     (dotimes (i n)
  1002.       (setf (aref result i) sound))
  1003.     result))
  1004.  
  1005.  
  1006. ;; (control sound)
  1007. ;;    Same as (sound sound), except this is used for control signals.  
  1008. ;;    This code is identical to sound.
  1009. (setfn control sound)
  1010.  
  1011.  
  1012. ;; (cue-file string)
  1013. ;;    Loads a sound file with the given name, returning a sound which is
  1014. ;; transformed to the current environment.
  1015. (defun cue-file (name)
  1016.     (cue (force-srate *SOUND-SRATE* (s-read name))))
  1017.  
  1018.  
  1019. ;; (env t1 t2 t4 l1 l2 l3 &optional duration)
  1020. ;; Creates a 4-phase envelope.
  1021. ;;    tN is the duration of phase N, and lN is the final level of
  1022. ;;    phase N.  t3 is implied by the duration, and l4 is 0.0.
  1023. ;;    If dur is not supplied, then 1.0 is assumed.  The envelope
  1024. ;;    duration is the product of dur, *STRETCH*, and *SUSTAIN*.  If 
  1025. ;;    t1 + t2 + 2ms + t4 > duration, then a two-phase envelope is
  1026. ;;    substituted that has an attack/release time ratio = t1/t4.
  1027. ;;    The sample rate of the returned sound is *CONTROL-SRATE*.
  1028. ;;
  1029. ;; Time transformation: the envelope is not warped; the start time and
  1030. ;; stop times are warped to global time.  Then the value of *SUSTAIN* at
  1031. ;; the begining of the envelope is used to determing absolute duration.
  1032. ;; Since PWL is ultimately called to create the envelope, we must use
  1033. ;; ABS-ENV to prevent any further transforms inside PWL.  We use
  1034. ;; (AT global-start ...) inside ABS-ENV so that the final result has 
  1035. ;; the proper starting time.
  1036. ;;
  1037. (defun env (t1 t2 t4 l1 l2 l3 &optional (duration 1.0))
  1038.   (let (actual-dur min-dur ratio t3
  1039.     (actual-dur (get-duration duration)))
  1040.     (setf min-dur (+ t1 t2 t4 0.002))
  1041.     (cond ((< actual-dur min-dur)
  1042.        (setf ratio (/ t1 (+ t1 t4)))
  1043.        (setf t1 (* ratio actual-dur))
  1044.        (setf t2 (- actual-dur t1))
  1045.        (setf t3 0.0)
  1046.        (setf t4 0.0)
  1047.        (setf l2 0.0)
  1048.        (setf l3 0.0))
  1049.       (t
  1050.        (setf t3 (- actual-dur t1 t2 t4))))
  1051.     (set-logical-stop
  1052.       (abs-env (at *rslt*
  1053.                    (pwl t1 l1 (+ t1 t2) l2 (- actual-dur t4) l3 actual-dur)))
  1054.       duration)))
  1055.  
  1056.  
  1057. (defun gate (sound lookahead risetime falltime floor threshold)
  1058.     (cond ((< lookahead risetime)
  1059.            (break "lookahead must be greater than risetime in GATE function"))
  1060.           ((or (< risetime 0) (< falltime 0) (< floor 0))
  1061.            (break "risetime, falltime, and floor must all be positive in GATE function"))
  1062.           (t
  1063.            (let ((s
  1064.               (snd-gate (seq (cue sound) (abs-env (s-rest lookahead)))
  1065.                     lookahead risetime falltime floor threshold)))
  1066.              (snd-xform s (snd-srate s) (snd-t0 sound) 
  1067.             (+ (snd-t0 sound) lookahead) MAX-STOP-TIME 1.0)))))
  1068.  
  1069.  
  1070. ;; (osc-note step &optional duration env sust volume sound)
  1071. ;;   Creates a note using table-lookup osc, but with an envelope.
  1072. ;; The ENV parameter may be a parameter list for the env function,
  1073. ;; or it may be a sound.
  1074. ;;
  1075. (defun osc-note (pitch &optional (duration 1.0) 
  1076.                (env-spec '(0.02 0.1 0.3 1.0 .8 .7))
  1077.                (volume 0.0)
  1078.                (table *TABLE*))
  1079.   (set-logical-stop
  1080.    (mult (loud volume (osc pitch duration table))
  1081.      (if (listp env-spec)
  1082.        (apply 'env env-spec)
  1083.        env-spec))
  1084.    duration))
  1085.  
  1086.  
  1087. ;; force-srate -- resample snd if necessary to get sample rate
  1088. ;
  1089. (defun force-srate (sr snd)
  1090.   (cond ((not (numberp sr))
  1091.      (error "force-srate: SR should be a number")))
  1092.   (cond ((arrayp snd)
  1093.      (let* ((len (length snd))
  1094.         (result (make-array len)))
  1095.        (dotimes (i len)
  1096.             (setf (aref result i) 
  1097.               (force-srate sr (aref snd i))))
  1098.        result))
  1099.     (t
  1100.      (let ((snd-sr (snd-srate snd)))
  1101.        (cond ((> sr snd-sr) (snd-up sr snd))
  1102.          ((< sr snd-sr) (snd-down sr snd))
  1103.          (t snd))))))
  1104.  
  1105.  
  1106. (defun force-srates (srs snd)
  1107.   (cond ((and (numberp srs) (soundp snd))
  1108.      (force-srate srs snd))
  1109.     ((and (arrayp srs) (arrayp snd))
  1110.      (let* ((len (length snd))
  1111.         (result (make-array len)))
  1112.        (dotimes (i len)
  1113.             (setf (aref result i) 
  1114.               (force-srate (aref srs i) (aref snd i))))
  1115.        result))
  1116.     (t (error "arguments not compatible"))))
  1117.  
  1118.  
  1119. ;; (breakpoints-convert (t1 x1 t2 x2 ... tn) t0)
  1120. ;;   converts times to sample numbers and scales amplitudes
  1121. ;;   t0 is the global (after warping) start time
  1122. ;;
  1123. ;; NOTE: there were some stack overflow problems with the original
  1124. ;; recursive version (in comments now), so it was rewritten as an
  1125. ;; iteration.
  1126. ;;
  1127. (defun breakpoints-convert (list t0)
  1128.   (prog (sample-count result sust (last-count 0))
  1129.     (setf sust (get-sustain))
  1130.  loop
  1131.     (setf sample-count 
  1132.       (truncate (+ 0.5 (* (- (local-to-global (* (car list) sust)) t0)
  1133.                  *control-srate*))))
  1134.     ; now we have a new sample count to put into result list
  1135.     ; make sure result is non-decreasing
  1136.     (cond ((< sample-count last-count)
  1137.        (setf sample-count last-count)))
  1138.     (setf last-count sample-count)
  1139.     (push sample-count result)
  1140.     (cond ((cdr list)
  1141.        (setf list (cdr list))
  1142.        (push (float (car list)) result)))
  1143.     (setf list (cdr list))
  1144.     (cond (list
  1145.        (go loop)))
  1146.     (return (reverse result))))
  1147.  
  1148.       
  1149.  
  1150. ;; (pwl t1 l1 t2 l2 ... tn)
  1151. ;;   Creates a piece-wise linear envelope from breakpoint data.
  1152. ;;
  1153. (defun pwl (&rest breakpoints) (pwl-list breakpoints))
  1154.  
  1155. (defun pwlr (&rest breakpoints) (pwlr-list breakpoints))
  1156.  
  1157. ;; (breakpoints-relative list)
  1158. ;;  converts list, which has the form (value dur value dur value ...)
  1159. ;;  into the form (value time value time value ...)
  1160. ;;  the list may have an even or odd length
  1161. ;;
  1162. (defun breakpoints-relative (breakpoints)
  1163.   (prog (result (sum 0.0))
  1164.  loop
  1165.      (cond (breakpoints
  1166.         (push (car breakpoints) result)
  1167.         (setf breakpoints (cdr breakpoints))
  1168.         (cond (breakpoints
  1169.            (setf sum (+ sum (car breakpoints)))
  1170.            (push sum result)
  1171.            (setf breakpoints (cdr breakpoints))
  1172.            (go loop)))))
  1173.      (return (reverse result))))
  1174.  
  1175.  
  1176. (defun breakpoints-relative (breakpoints)
  1177.   (prog (result (sum 0.0))
  1178.  loop
  1179.     (setf sum (+ sum (car breakpoints)))
  1180.     (push sum result)
  1181.     (cond ((cdr breakpoints)
  1182.        (setf breakpoints (cdr breakpoints))
  1183.        (push (car breakpoints) result)))
  1184.     (setf breakpoints (cdr breakpoints))
  1185.     (cond (breakpoints
  1186.        (go loop)))
  1187.     (return (reverse result))))
  1188.  
  1189.  
  1190. (defun pwlr-list (breakpoints)
  1191.   (pwl-list (breakpoints-relative breakpoints)))
  1192.  
  1193. (defun pwl-list (breakpoints)
  1194.   (let ((t0 (local-to-global 0)))
  1195.     (snd-pwl t0 *control-srate* (breakpoints-convert breakpoints t0))))
  1196.  
  1197. ;; (pwlv l1 t1 l2 t2 ... ln)
  1198. ;; Creates a piece-wise linear envelope from breakpoint data;
  1199. ;; the function initial and final values are explicit
  1200. ;;
  1201. (defun pwlv (&rest breakpoints)
  1202.   ;use pwl, modify breakpoints with initial and final changes
  1203.   ;need to put initial time of 0, and final time of 0
  1204.   (pwlv-list breakpoints))
  1205.  
  1206. (defun pwlv-list (breakpoints)
  1207.     (pwl-list (cons 0.0 (append breakpoints '(0.0)))))
  1208.  
  1209. (defun pwlvr (&rest breakpoints) (pwlvr-list breakpoints))
  1210.  
  1211. (defun pwlvr-list (breakpoints)
  1212.   (pwlr-list (cons 0.0 (append breakpoints '(0.0)))))
  1213.  
  1214. (defun pwe (&rest breakpoints)
  1215.   (pwe-list breakpoints))
  1216.  
  1217. (defun pwe-list (breakpoints)
  1218.   (pwev-list (cons 1.0 (append breakpoints '(1.0)))))
  1219.  
  1220. (defun pwer (&rest breakpoints) (pwer-list breakpoints))
  1221.  
  1222. (defun pwer-list (breakpoints)
  1223.   (pwe-list (breakpoints-relative breakpoints)))
  1224.  
  1225. (defun pwev (&rest breakpoints)
  1226.   (pwev-list breakpoints))
  1227.  
  1228. (defun pwev-list (breakpoints)
  1229.   (let ((lis (breakpoints-log breakpoints)))
  1230.     (s-exp (pwl-list lis))))
  1231.  
  1232. (defun pwevr (&rest breakpoints) (pwevr-list breakpoints))
  1233.  
  1234. (defun pwevr-list (breakpoints)
  1235.   (pwev-list (cdr (breakpoints-relative (cons 0.0 breakpoints)))))
  1236.  
  1237.  
  1238. (defun breakpoints-log (breakpoints)
  1239.   (prog ((result '(0.0)) val tim)
  1240. loop
  1241.     (cond (breakpoints
  1242.        (setf val (float (car breakpoints)))
  1243.        (setf breakpoints (cdr breakpoints))
  1244.        (cond (breakpoints
  1245.           (setf tim (car breakpoints))
  1246.           (setf breakpoints (cdr breakpoints))))
  1247.        (setf result (cons tim (cons (log val) result)))
  1248.        (cond ((null breakpoints)
  1249.           (return (reverse result))))
  1250.        (go loop))
  1251.       (t
  1252.        (error "Expected odd number of elements in breakpoint list")))))
  1253.  
  1254.  
  1255. ;; SOUND-WARP -- apply warp function to a sound
  1256. ;; 
  1257. (defun sound-warp (warp-fn signal &optional wrate)
  1258.   (cond (wrate
  1259.      (snd-resamplev signal *sound-srate*
  1260.             (snd-inverse warp-fn (local-to-global 0) wrate)))
  1261.     (t
  1262.      (snd-compose signal 
  1263.               (snd-inverse warp-fn (local-to-global 0) *sound-srate*)))))
  1264.  
  1265. (defun snd-extent (sound maxsamples) 
  1266.     (list (snd-t0 sound)
  1267.       (+ (snd-t0 sound) (/ (snd-length sound maxsamples)
  1268.                    (snd-srate sound)))))
  1269.  
  1270. (setfn snd-flatten snd-length)
  1271.  
  1272. ;; (maketable sound)
  1273. ;;   Creates a table for osc, lfo, etc. by assuming that the samples
  1274. ;;   in sound represent one period.  The sound must start at time 0.
  1275.  
  1276. (defun maketable (sound)
  1277.   (list sound
  1278.     (hz-to-step 
  1279.      (/ 1.0
  1280.         (cadr (snd-extent sound 1000000))))
  1281.     T))
  1282.  
  1283.  
  1284. ;(defmacro endTime (sound)
  1285. ;   `(get-logical-stop ,sound))
  1286.  
  1287.  
  1288. ;(defmacro beginTime (sound)
  1289. ;   `(car (snd-extent ,sound)))
  1290.  
  1291.  
  1292. ; simple stereo pan: as where goes from 0 to 1, sound
  1293. ; is linearly panned from left to right
  1294. ;
  1295. (defun pan (sound where)
  1296.   (vector (mult sound (sum 1 (mult -1 where)))
  1297.       (mult sound where)))
  1298.  
  1299.  
  1300. (defun prod (&rest snds)
  1301.   (cond ((null snds)
  1302.      (snd-zero (local-to-global 0) *sound-srate*))
  1303.     ((null (cdr snds))
  1304.      (car snds))
  1305.     ((null (cddr snds))
  1306.      (nyq:prod2 (car snds) (cadr snds)))
  1307.     (t
  1308.      (nyq:prod2 (car snds) (apply #'prod (cdr snds))))))
  1309.  
  1310. (setfn mult prod)
  1311.  
  1312.  
  1313. ;; (NYQ:PROD-OF-ARRAYS S1 S2) - form pairwise products
  1314. ;
  1315. (defun nyq:prod-of-arrays (s1 s2)
  1316.   (let* ((n (length s1))
  1317.      (p (make-array n)))
  1318.     (cond ((/= n (length s2))
  1319.        (error "unequal number of channels in prod")))
  1320.     (dotimes (i n)
  1321.       (setf (aref p i) (nyq:prod2 (aref s1 i) (aref s2 i))))
  1322.     p))
  1323.  
  1324.  
  1325. ; nyq:prod2 - multiply two arguments
  1326. (defun nyq:prod2 (s1 s2)
  1327.   (setf s1 (nyq:coerce-to s1 s2))
  1328.   (setf s2 (nyq:coerce-to s2 s1))
  1329.   (cond ((arrayp s1)
  1330.      (nyq:prod-of-arrays s1 s2))
  1331.     (t
  1332.      (nyq:prod-2-sounds s1 s2))))
  1333.  
  1334.  
  1335. ; (PROD-2-SOUNDS S1 S2) - multiply two sound arguments
  1336. (defun nyq:prod-2-sounds (s1 s2)
  1337.   (cond ((numberp s1)
  1338.      (cond ((numberp s2)
  1339.         (* s1 s2))
  1340.            (t
  1341.         (scale s1 s2))))
  1342.     ((numberp s2)
  1343.      (scale s2 s1))
  1344.     (t
  1345.      (let ((s1sr (snd-srate s1))
  1346.            (s2sr (snd-srate s2)))
  1347. ;    (display "nyq:prod-2-sounds" s1sr s2sr)
  1348.         (cond ((> s1sr s2sr)
  1349.            (snd-prod s1 (snd-up s1sr s2)))
  1350.           ((< s1sr s2sr)
  1351.            (snd-prod (snd-up s2sr s1) s2))
  1352.           (t
  1353.            (snd-prod s1 s2)))))))
  1354.  
  1355.  
  1356. ;; RAMP -- linear ramp from 0 to x
  1357. ;;
  1358. (defun ramp (&optional (x 1))
  1359.   (let* ((duration (get-duration x)))
  1360.     (set-logical-stop
  1361.       (warp-abs nil
  1362.         (at *rslt*
  1363.           (sustain-abs 1
  1364.                        (pwl duration 1 (+ duration (/ *control-srate*))))))
  1365.       x)))
  1366.  
  1367.  
  1368. (defun resample (snd rate)
  1369.   (cond ((arrayp snd)
  1370.      (let* ((len (length snd))
  1371.         (result (make-array len)))
  1372.         (dotimes (i len)
  1373.         (setf (aref result i)
  1374.               (snd-resample (aref snd i) rate)))
  1375.         result))
  1376.     (t
  1377.      (snd-resample snd rate))))
  1378.  
  1379.  
  1380. (defun scale (amt snd)
  1381.   (cond ((arrayp snd)
  1382.      (let* ((len (length snd))
  1383.         (result (make-array len)))
  1384.         (dotimes (i len)
  1385.         (setf (aref result i) (snd-scale amt (aref snd i))))
  1386.         result))
  1387.     (t
  1388.      (snd-scale amt snd))))
  1389.  
  1390.  
  1391. (setfn s-print-tree snd-print-tree)
  1392.  
  1393. ;; (PEAK sound-expression number-of-samples) - find peak amplitude
  1394. ;
  1395. ; NOTE: this used to be called s-max
  1396. ;
  1397. (defmacro peak (expression maxlen)
  1398.    `(snd-max ',expression ,maxlen))
  1399.  
  1400. ;; (S-MAX S1 S2) - return maximum of S1, S2
  1401. ;
  1402. (defun s-max (s1 s2)
  1403.   (setf s1 (nyq:coerce-to s1 s2))
  1404.   (setf s2 (nyq:coerce-to s2 s1))
  1405.   (cond ((arrayp s1)
  1406.      (nyq:max-of-arrays s1 s2))
  1407.     (t
  1408.      (nyq:max-2-sounds s1 s2))))
  1409.  
  1410. (defun nyq:max-of-arrays (s1 s2)
  1411.   (let* ((n (length s1))
  1412.      (p (make-array n)))
  1413.     (cond ((/= n (length s2))
  1414.        (error "unequal number of channels in max")))
  1415.     (dotimes (i n)
  1416.       (setf (aref p i) (s-max (aref s1 i) (aref s2 i))))
  1417.     p))
  1418.  
  1419. (defun nyq:max-2-sounds (s1 s2)
  1420.   (cond ((numberp s1)
  1421.          (cond ((numberp s2)
  1422.                 (max s1 s2))
  1423.                (t
  1424.                 (snd-maxv s2
  1425.                           (snd-const s1 (local-to-global 0.0)
  1426.                                      (snd-srate s2) (get-duration 1.0))))))
  1427.         ((numberp s2)
  1428.          (snd-maxv s1 (snd-const s2 (local-to-global 0.0)
  1429.                    (snd-srate s1) (get-duration 1.0))))
  1430.         (t
  1431.          (let ((s1sr (snd-srate s1))
  1432.                (s2sr (snd-srate s2)))
  1433.             (cond ((> s1sr s2sr)
  1434.                    (snd-maxv s1 (snd-up s1sr s2)))
  1435.                   ((< s1sr s2sr)
  1436.                    (snd-maxv (snd-up s2sr s1) s2))
  1437.                   (t
  1438.                    (snd-maxv s1 s2)))))))
  1439.  
  1440. (defun s-min (s1 s2)
  1441.   (setf s1 (nyq:coerce-to s1 s2))
  1442.   (setf s2 (nyq:coerce-to s2 s1))
  1443.   (cond ((arrayp s1)
  1444.          (nyq:min-of-arrays s1 s2))
  1445.         (t
  1446.          (nyq:min-2-sounds s1 s2))))
  1447.  
  1448. (defun nyq:min-of-arrays (s1 s2)
  1449.   (let* ((n (length s1))
  1450.      (p (make-array n)))
  1451.     (cond ((/= n (length s2))
  1452.        (error "unequal number of channels in max")))
  1453.     (dotimes (i n)
  1454.       (setf (aref p i) (s-min (aref s1 i) (aref s2 i))))
  1455.     p))
  1456.  
  1457. (defun nyq:min-2-sounds (s1 s2)
  1458.   (cond ((numberp s1)
  1459.          (cond ((numberp s2)
  1460.                 (min s1 s2))
  1461.                (t
  1462.                 (snd-minv s2
  1463.                           (snd-const s1 (local-to-global 0.0)
  1464.                                      (snd-srate s2) (get-duration 1.0))))))
  1465.         ((numberp s2)
  1466.          (snd-minv s1 (snd-const s2 (local-to-global 0.0)
  1467.                    (snd-srate s1) (get-duration 1.0))))
  1468.        (t
  1469.         (let ((s1sr (snd-srate s1))
  1470.               (s2sr (snd-srate s2)))
  1471.           (cond ((> s1sr s2sr)
  1472.                  (snd-minv s1 (snd-up s1sr s2)))
  1473.                 ((< s1sr s2sr)
  1474.                  (snd-minv (snd-up s2sr s1) s2))
  1475.                (t
  1476.                 (snd-minv s1 s2)))))))
  1477.  
  1478. (defun snd-minv (s1 s2)
  1479.   (scale -1.0 (snd-maxv (scale -1.0 s1) (scale -1.0 s2))))
  1480.  
  1481. ; sequence macros SEQ and SEQREP are now in seq.lsp:
  1482. (load "seq" :verbose NIL)
  1483.  
  1484.  
  1485. ; set-logical-stop - modify the sound and return it, time is shifted and
  1486. ;             stretched
  1487. (defun set-logical-stop (snd tim)
  1488.   (let ((d (local-to-global tim)))
  1489.     (multichan-expand #'set-logical-stop-abs snd d)))
  1490.  
  1491.  
  1492. ; set-logical-stop-abs - modify the sound and return it
  1493. (defun set-logical-stop-abs (snd tim) (snd-set-logical-stop snd tim) snd)
  1494.  
  1495.  
  1496. (defmacro simrep (pair sound)
  1497.   `(let (_snds)
  1498.      (dotimes ,pair (push ,sound _snds))
  1499.      (sim-list _snds)))
  1500.  
  1501. (defun sim (&rest snds)
  1502.   (sim-list snds))
  1503.  
  1504. (setfn sum sim)
  1505.  
  1506. (defun sim-list (snds)
  1507.   (cond ((null snds)
  1508.          (snd-zero (local-to-global 0) *sound-srate*))
  1509.         ((null (cdr snds))
  1510.          (car snds))
  1511.         ((null (cddr snds))
  1512.          (nyq:add2 (car snds) (cadr snds)))
  1513.         (t
  1514.          (nyq:add2 (car snds) (sim-list (cdr snds))))))
  1515.  
  1516.  
  1517. (defun s-rest (&optional (dur 1.0) (chans 1))
  1518.   (let ((d (get-duration dur))
  1519.         r)
  1520.     (cond ((= chans 1)
  1521.            (snd-const 0.0 *rslt* *SOUND-SRATE* d))
  1522.           (t
  1523.            (setf r (make-array chans))
  1524.            (dotimes (i chans)
  1525.              (setf (aref r i) (snd-const 0.0 *rslt* *SOUND-SRATE* d)))
  1526.            r))))
  1527.  
  1528.  
  1529. (defun tempo (warpfn)
  1530.   (slope (snd-inverse warpfn (local-to-global 0) *control-srate*)))
  1531.  
  1532.  
  1533.  
  1534. ;; (SUM-OF-ARRAYS S1 S2) - add multichannel sounds
  1535. ; result has as many channels the largest of s1, s2
  1536. ; corresponding channels are added, extras are copied
  1537. (defun sum-of-arrays (s1 s2)
  1538.   (let* ((n1 (length s1))
  1539.      (n2 (length s2))
  1540.      (n (min n1 n2))
  1541.      (m (max n1 n2))
  1542.      (result (make-array m))
  1543.      (big-s (if (> n1 n2) s1 s2)))
  1544.     
  1545.     (dotimes (i n)
  1546.       (setf (aref result i) (nyq:add-2-sounds (aref s1 i) (aref s2 i))))
  1547.     (dotimes (i (- m n))
  1548.       (setf (aref result (+ n i)) (aref big-s (+ n i))))
  1549.     result))
  1550.  
  1551.  
  1552. ;; (WARP fn behavior) - warp behavior according to fn
  1553. ;;
  1554. ;; fn is a map from behavior time to local time, and *WARP* expresses
  1555. ;; a map from local to global time.
  1556. ;; To produce a new *WARP* for the environment, we want to compose the
  1557. ;; effect of the current *WARP* with fn.  Note that fn is also a behavior.
  1558. ;; It is evaluated in the current environment first, then it is used to
  1559. ;; modify the environment seen by behavior.
  1560. ;; *WARP* is a triple: (d s f) denoting the function f(st+d).
  1561. ;; Letting g represent the new warp function fn, we want f(st+d) o g, or
  1562. ;; f(s*g(t) + d) in the form (d' s' f').
  1563. ;; Let's do this one step at a time:
  1564. ;; f(s*g(t) + d) = f(scale(s, g) + d)
  1565. ;;               = (shift f -d)(scale(s, g))
  1566. ;;               = (snd-compose (shift-time f (- d)) (scale s g))
  1567. ;;
  1568. ;; If f in NIL, it denotes the identity mapping f(t)=t, so we can
  1569. ;; simplify:
  1570. ;; f(scale(s, g) + d) = scale(s, g) + d
  1571. ;;                    = (snd-offset (scale s g) d)
  1572.  
  1573. (defmacro warp (x s)
  1574.  `(progv '(*WARP*) (list 
  1575.             (list 0.0 1.0
  1576.               (if (warp-function *WARP*)
  1577.                   (snd-compose (shift-time (warp-function *WARP*) 
  1578.                                (- (warp-time *WARP*)))
  1579.                        (scale (warp-stretch *WARP*) 
  1580.                           (must-be-sound ,x)))
  1581.                   (snd-offset (scale (warp-stretch *WARP*) 
  1582.                          (must-be-sound ,x))
  1583.                       (warp-time *WARP*)))))
  1584.      ,s))
  1585.  
  1586.  
  1587. (defmacro warp-abs (x s)
  1588.  `(progv '(*WARP*) (list (list 0.0 1.0 ,x))
  1589.      ,s))
  1590.  
  1591.  
  1592. ;; MULTICHAN-EXPAND -- construct and return array according to args
  1593. ;;
  1594. ;; arrays are used in Nyquist to represent multiple channels
  1595. ;; if any argument is an array, make sure all array arguments
  1596. ;; have the same length.  Then, construct a multichannel result
  1597. ;; by calling fn once for each channel.  The arguments passed to
  1598. ;; fn for the i'th channel are either the i'th element of an array
  1599. ;; argument, or just a copy of a non-array argument.
  1600. ;;
  1601. (defun multichan-expand (fn &rest args)
  1602.   (let (len newlen result) ; len is a flag as well as a count
  1603.     (dolist (a args)
  1604.         (cond ((arrayp a)
  1605.            (setf newlen (length a))
  1606.            (cond ((and len (/= len newlen))
  1607.               (error (format nil "In ~A, two arguments are vectors of differing length." fn))))
  1608.            (setf len newlen))))
  1609.     (cond (len
  1610.        (setf result (make-array len))
  1611.        ; for each channel, call fn with args
  1612.        (dotimes (i len)
  1613.            (setf (aref result i)
  1614.              (apply fn
  1615.             (mapcar
  1616.                 #'(lambda (a)
  1617.                 ; take i'th entry or replicate:
  1618.                 (cond ((arrayp a) (aref a i))
  1619.                       (t a)))
  1620.                 args))))
  1621.        result)
  1622.       (t
  1623.        (apply fn args)))))
  1624.  
  1625.  
  1626. ;; SELECT-IMPLEMENTATION-? -- apply an implementation according to args
  1627. ;;
  1628. ;; There is a different Nyquist primitive for each combination of 
  1629. ;; constant (NUMBERP) and time-variable (SOUNDP) arguments.  E.g.
  1630. ;; a filter with fixed parameters differs from one with varying
  1631. ;; parameters.  In most cases, the user just calls one function,
  1632. ;; and the arguments are decoded here:
  1633.  
  1634.  
  1635. ;; SELECT-IMPLEMENTATION-1-1 -- 1 sound arg, 1 selector
  1636. ;;
  1637. (defun select-implementation-1-1 (fns snd sel1 &rest others)
  1638.   (if (numberp sel1)
  1639.     (apply (aref fns 0) (cons snd (cons sel1 others)))
  1640.     (apply (aref fns 1) (cons snd (cons sel1 others)))))
  1641.  
  1642.  
  1643. ;; SELECT-IMPLEMENTATION-1-2 -- 1 sound arg, 2 selectors
  1644. ;;
  1645. ;; choose implemenation according to args 2 and 3
  1646. ;;
  1647. (defun select-implementation-1-2 (fns snd sel1 sel2 &rest others)
  1648.   (if (numberp sel2)
  1649.     (if (numberp sel1)
  1650.       (apply (aref fns 0) (cons snd (cons sel1 (cons sel2 others))))
  1651.       (apply (aref fns 1) (cons snd (cons sel1 (cons sel2 others)))))
  1652.     (if (numberp sel1)
  1653.       (apply (aref fns 2) (cons snd (cons sel1 (cons sel2 others))))
  1654.       (apply (aref fns 3) (cons snd (cons sel1 (cons sel2 others)))))))
  1655.  
  1656. ;; some waveforms
  1657.  
  1658. (setf *saw-table* (pwlvr -1 1 1))        ; eh, creepy way to get 2205 samples.
  1659. (setf *saw-table* (list *saw-table* (hz-to-step 1) T))
  1660.  
  1661. (setf *tri-table* (pwlvr -1 0.5 1 0.5 -1))
  1662. (setf *tri-table* (list *tri-table* (hz-to-step 1) T))
  1663.  
  1664. (setf *id-shape*  (pwlvr -1 2 1 .01 1))                ; identity
  1665. (setf *step-shape* (seq (const -1) (const 1 1.01)))  ; hard step at zero
  1666.  
  1667. (defun exp-dec (hold halfdec length)
  1668.   (let* ((target (expt 0.5 (/ length halfdec)))
  1669.      (expenv (pwev 1 hold 1 length target)))
  1670.     expenv)
  1671. )
  1672.  
  1673. ;;; operations on sounds
  1674.  
  1675. (defun diff (x &optional y)
  1676.   (cond (y (sum x (prod -1 y)))
  1677.         (t (prod -1 x))))
  1678.  
  1679. ; compare-shape is a shape table -- origin 1.
  1680. (defun compare (x y &optional (compare-shape *step-shape*))
  1681.   (let ((xydiff (diff x y)))
  1682.     (shape xydiff compare-shape 1)))
  1683.  
  1684. ;;; oscs
  1685.  
  1686. (defun osc-saw (hz) (hzosc hz *saw-table*))
  1687. (defun osc-tri (hz) (hzosc hz *tri-table*))
  1688.  
  1689. ; bias is [-1, 1] pulse width.  sound or scalar.
  1690. ; hz is a sound or scalar
  1691. (defun osc-pulse (hz bias &optional (compare-shape *step-shape*))
  1692.   (compare bias (osc-tri hz) compare-shape))
  1693.   
  1694. ;;; tapped delays
  1695.  
  1696. ;(tapv snd offset vardelay maxdelay)
  1697. (setfn tapv snd-tapv) ;; linear interpolation
  1698. (setfn tapf snd-tapf) ;; no interpolation
  1699.